package com.haidechizi.leetcode;

import java.util.*;

/**
 * @author: wangdejun
 * @create: 2020-04-13 09:57
 **/
public class _355_设计推特 {

    /**
     * 存储用户发布的推特
     */
    List<TweetInfo> tweetInfos = new LinkedList<>();

    Map<Integer, List<Integer>> followMap = new HashMap<>();

    public void postTweet(int userId, int tweetId) {
        TweetInfo tweetInfo = new TweetInfo(userId, tweetId);
        tweetInfos.add(0, tweetInfo);
    }

    public List<Integer> getNewsFeed(int userId) {
        List<Integer> result = new ArrayList<>();
        List<Integer> followList = followMap.getOrDefault(userId, new ArrayList<>());
        followList.add(userId);
        for (TweetInfo tweetInfo : tweetInfos) {
            if (followList.contains(tweetInfo.userId)) {
                result.add(tweetInfo.tweetId);
                if (result.size() >= 10) {
                    break;
                }
            }
        }
        return result;
    }

    public void follow(int followerId, int followeeId) {
        List<Integer> followList = followMap.getOrDefault(followerId, new ArrayList<>());
        followList.add(followeeId);
        followMap.put(followerId, followList);
    }

    public void unfollow(int followerId, int followeeId) {
        List<Integer> followList = followMap.get(followerId);
        if (followList == null || followList.size() == 0) {
            return;
        }
        followList.remove((Integer) followeeId);
    }

    private static class TweetInfo {
        Integer userId;
        Integer tweetId;

        public TweetInfo(Integer userId, Integer tweetId) {
            this.userId = userId;
            this.tweetId = tweetId;
        }
    }

}
